//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.common.cas;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.constant.HttpStatus;
import com.ruoyi.common.core.domain.R;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jasig.cas.client.authentication.AuthenticationRedirectStrategy;
import org.jasig.cas.client.util.CommonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class DefaultAjaxAuthenticationRedirectStrategy implements AuthenticationRedirectStrategy {
    private static final Logger log = LoggerFactory.getLogger(DefaultAjaxAuthenticationRedirectStrategy.class);

    public DefaultAjaxAuthenticationRedirectStrategy() {
    }

    @Override
    public void redirect(HttpServletRequest request, HttpServletResponse response, String potentialRedirectUrl) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        PrintWriter writer = response.getWriter();
        Throwable var5 = null;

        try {
            R rlt = R.ok(this.getRedirectUrl(request));
            rlt.setCode(HttpStatus.CAS);
            writer.write((new ObjectMapper()).writeValueAsString(rlt));
        } catch (Throwable var14) {
            var5 = var14;
            throw var14;
        } finally {
            if (writer != null) {
                if (var5 != null) {
                    try {
                        writer.close();
                    } catch (Throwable var13) {
                        var5.addSuppressed(var13);
                    }
                } else {
                    writer.close();
                }
            }

        }

    }

    private String getRedirectUrl(HttpServletRequest request) {
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
        CasConfiguration casConfiguration = (CasConfiguration)applicationContext.getBean(CasConfiguration.class);
        String callbackUrl = getCallbackUrl(request, casConfiguration.getHomePage());
        String service = casConfiguration.getCasClientLoginUrl() + "?clientPage=" + callbackUrl;
        String urlToRedirectTo = CommonUtils.constructRedirectUrl(casConfiguration.getCasServerLoginUrl(), "service", service, false, false);
        return urlToRedirectTo;
    }

    public static String getCallbackUrl(HttpServletRequest request, String defaultPage) {
        String callbackUrl = request.getHeader("callbackUrl");
        callbackUrl = callbackUrl == null ? request.getParameter("callbackUrl") : callbackUrl;
        callbackUrl = callbackUrl == null ? defaultPage : callbackUrl;
        callbackUrl = callbackUrl == null ? request.getRequestURL().toString() : callbackUrl;
        return callbackUrl;
    }
}
